﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.geom.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.models.world.vos.areas.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;

    public class MoveToTargetRoomCommand extends AbstractMoveCommand {

		[Inject]
        public var userModel:UserModel;
        private var _mover:IMoving;
        private var _targetRoute:Array;
        private var _newTargetRoom:IRoom;

        override public function execute():void{
            var _local3:Vector.<Point>;
            var _local4:IRoom;
            var _local5:Point;
            var _local6:Point;
            var _local7:Point;
            var _local8:int;
            var _local1:BuildingVO = world.building;
            this._mover = IMoving(event.info);
            var _local2:String = ((this._mover.targetType) || (GameConstants.TYPE_RECEPTION));
            this._targetRoute = roomSlotsModel.getNearestFreeRoom(this._mover.roomId, _local2, _local1, this._mover.id);
            if (!this._targetRoute){
                _local1.addToQueue(_local2, (this._mover as ITreatable));
                switch (_local2){
                    case GameConstants.TYPE_RECEPTION:
                    case GameConstants.TYPE_KINDERGARTEN:
                        this._mover.targetType = GameConstants.TYPE_ENTRANCE_AREA;
                        break;
                    default:
                        this._mover.targetType = GameConstants.TYPE_WAITING_ROOM;
                        break;
                };
                this._targetRoute = roomSlotsModel.getNearestFreeRoom(this._mover.roomId, this._mover.targetType, _local1, this._mover.id);
            };
            if (this._targetRoute.length == 0){
                _local3 = new Vector.<Point>();
                _local4 = this.getTargetRoom(this._mover.roomId);
                _local4.reservedBy = this._mover.id;
                _local5 = _local4.getTargetPositionOf(this._mover.state);
                _local6 = roomSlotsModel.getSlotById(_local4.slotId).position;
                _local7 = new Point((_local6.x + _local5.x), (_local6.y + _local5.y));
                _local3.push(_local7);
                dispatch(new InfoEvent(EventConstants.MOVE_TO_POSITION, this._mover.id, _local3));
            } else {
                _local8 = int(this._targetRoute[(this._targetRoute.length - 1)]);
                this._newTargetRoom = getRoomBySlotId(_local8, _local1.rooms);
                this.getTargetRoom(this._newTargetRoom.id).reservedBy = this._mover.id;
                if (this._mover.targetRoomId != this._newTargetRoom.id){
                    this.storeLocation();
                };
                this.moveToTarget();
            };
        }
        private function storeLocation():void{
            var _local1:Object;
            var _local2:RequestEvent;
            if (!this.userModel.isVisitFriendsMode()){
                this._mover.targetRoomId = this._newTargetRoom.id;
                _local1 = {};
                _local1.id = this._mover.id;
                _local1[ProtocolConstants.ROOM_ID] = this._newTargetRoom.id;
                _local2 = new RequestEvent(EventConstants.SEND_REQUEST, this.onSetLocation);
                _local2.addRequest("set_location", _local1);
                dispatch(_local2);
            };
        }
        private function onSetLocation(_arg1:ResultVO):void{
            if (!_arg1.success){
                this.getTargetRoom(this._newTargetRoom.id).reservedBy = null;
            };
        }
        private function moveToTarget():void{
            var _local1:int = int(this._targetRoute[0]);
            var _local2:IRoom = getRoomBySlotId(_local1, world.building.rooms);
            this._mover.nextRoomId = _local2.id;
            var _local3:int = this.getTargetRoom(this._mover.roomId).slotId;
            var _local4:Vector.<Point> = getNextPosition(_local3, this._targetRoute[0]);
            dispatch(new InfoEvent(EventConstants.MOVE_TO_POSITION, this._mover.id, _local4));
        }
        private function getTargetRoom(_arg1:String):IRoom{
            var _local2:IRoom = IRoom(world.getRoomById(_arg1));
            return (_local2);
        }

    }
}//package net.wooga.pets.controllers.world 
